//
//  ZYCAttendanceSummaryDetailViewController.m
//  ZDSC
//
//  Created by 张威 on 2021/10/26.
//

#import "ZYCAttendanceSummaryDetailViewController.h"
#import "ZYCTourRecordMenuView.h"
#import "ZYCAttendanceSummaryCell.h"
#import "ZYCAttendanceSingleDetailsViewController.h"
#import "ZYCAttendanceSummaryDetailModel.h"
#import "ZYCCommonSelectObjectPickerView.h"
#import "ZYCSelectPastYearMonthView.h"
#import "ZYCCommonBaseModel.h"
#import "ZYCNoDataView.h"
#import "NSDateExtra.h"

@interface ZYCAttendanceSummaryDetailViewController ()<UITableViewDataSource,UITableViewDelegate,ZYCTourRecordMenuViewDelegate,ZYCCommonSelectObjectPickerViewDelegate>

@property (nonatomic, strong) ZYCTourRecordMenuView *menuView;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, strong) ZYCCommonSelectObjectPickerView *postPickerView;
@property (nonatomic, strong) ZYCSelectPastYearMonthView *dateSelectView;
@property (nonatomic, strong) ZYCNoDataView *noDataView;
@end

@implementation ZYCAttendanceSummaryDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = self.titleString;
    NSString *leftStr = [NSString stringWithFormat:@"%@", [NSDateExtra getDateStringWithDateString:self.attendanceDate currenDateFormat:@"yyyy-MM-dd" dateFormat:@"yyyy年MM月"]];
    self.menuView.leftStr = leftStr;
    self.menuView.rightStr = self.postName;
    [self.view addSubview:self.menuView];
    [self.view addSubview:self.tableView];
    [self loadAttendanceDetail];
    
}

- (void)loadAttendanceDetail{
    NSDictionary *params = @{
        @"attendanceDate":self.attendanceDate,
        @"postId":self.postId,
        @"status":self.status
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_TeacherMonthStatusCensus requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCAttendanceSummaryDetailModel class] json:responseObject[@"body"]];
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:array];
                if (self.dataSource.count == 0) {
                    self.noDataView.hidden = NO;
                    self.tableView.hidden = YES;
                }else{
                    self.noDataView.hidden = YES;
                    self.tableView.hidden = NO;
                }
                [self.tableView reloadData];
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataSource.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    ZYCAttendanceSummaryCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([ZYCAttendanceSummaryCell class]) forIndexPath:indexPath];
    cell.status = self.status;
    cell.model = self.dataSource[indexPath.section];
    return cell;
}

-(CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        return 5.0f;
    }
    return 1.5f;
}

-(CGFloat) tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    ZYCAttendanceSingleDetailsViewController *vc = [ZYCAttendanceSingleDetailsViewController new];
    ZYCAttendanceSummaryDetailModel *model = self.dataSource[indexPath.section];
    vc.titleString = model.teacherName;
    vc.typeString = self.typeString;
    vc.teacherId = model.teacherId;
    vc.attendanceDate = self.attendanceDate;
    vc.status = self.status;
    vc.postArr = self.postArr;
    [self.navigationController pushViewController:vc animated:YES];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    return [UIView new];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    return [UIView new];
}


#pragma mark - ZYCTourRecordMenuViewDelegate
- (void)tourRecordMenuViewClickLeft{
    [self.dateSelectView show];
}
- (void)tourRecordMenuViewClickRight{
    [self.postPickerView show];
}

#pragma mark - ZYCCommonSelectObjectPickerViewDelegate
- (void)selectObjectPickerView:(ZYCCommonSelectObjectPickerView *)pickerView DidClickModel:(ZYCCommonBaseModel *)model{
    if (pickerView == self.postPickerView) {
        self.postId = model.nameId;
        self.postName = model.name;
        self.menuView.rightStr = model.name;
        [self loadAttendanceDetail];
    }
}
#pragma mark - getters
- (ZYCNoDataView *)noDataView {
    if (!_noDataView) {
        _noDataView = [[ZYCNoDataView alloc] initWithFrame:self.tableView.frame];
        _noDataView.hidden = YES;
        [self.view addSubview:_noDataView];
    }
    return _noDataView;
}

- (ZYCCommonSelectObjectPickerView *)postPickerView {
    if (!_postPickerView) {
        _postPickerView = [[ZYCCommonSelectObjectPickerView alloc] init];
        _postPickerView.tipString = @"请选择岗位";
        _postPickerView.dataSource = self.postArr;
        _postPickerView.delegate = self;
        [self.navigationController.view addSubview:_postPickerView];
    }
    return _postPickerView;
}

- (ZYCSelectPastYearMonthView *)dateSelectView {
    ZYCWeakSelf
    if (!_dateSelectView) {
        _dateSelectView = [[ZYCSelectPastYearMonthView alloc] init];
        _dateSelectView.tipString = @"请选择时间";
        _dateSelectView.currentDate = [NSDate date];
        _dateSelectView.confirmAction = ^(NSString * _Nonnull dateStr) {
            weakSelf.attendanceDate = [NSString stringWithFormat:@"%@-01",dateStr];
            weakSelf.menuView.leftStr = [NSString stringWithFormat:@"%@", [NSDateExtra getDateStringWithDateString:dateStr currenDateFormat:@"yyyy-MM" dateFormat:@"yyyy年MM月"]];
            [weakSelf loadAttendanceDetail];
        };
        [self.navigationController.view addSubview:_dateSelectView];
    }
    return _dateSelectView;
}
- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}

- (NSDateFormatter *)dateFormatter {
    if (!_dateFormatter) {
        _dateFormatter = [[NSDateFormatter alloc] init];
        [_dateFormatter setDateFormat:@"yyyy-MM-dd"];
    }
    return _dateFormatter;
}
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(self.menuView.frame), kScreenWidth, kScreenHeight - CGRectGetMaxY(self.menuView.frame)-safeAreaBottomHeight) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [_tableView registerClass:[ZYCAttendanceSummaryCell class] forCellReuseIdentifier:NSStringFromClass([ZYCAttendanceSummaryCell class])];
    }
    return _tableView;
}


- (ZYCTourRecordMenuView *)menuView {
    if (!_menuView) {
        _menuView = [[ZYCTourRecordMenuView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, 46.0f)];

        _menuView.delegate = self;
    }
    return _menuView;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
